#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "ItemDetailsForm.h"



#include <Defines.h>
#include <CampaignItem.h>
#include <ItemModel.h>
#include <ItemGroup.h>
#include <Item.h>



#include <XmlSerializer.h>
#include <FileHandler.h>


#include <QMessageBox>
#include <QFileDialog>
#include <QTreeView>
#include <QTimer>


namespace
{
QString NEW_CAMPAIGN_NAME = "New Campaign";
}



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    FileHandler::installSerializer(new XmlSerializer());

    ui->setupUi(this);
}



MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::closeEvent(QCloseEvent *e)
{
    on_actionQuit_triggered();

    QMainWindow::closeEvent(e);
}



void MainWindow::resizeEvent(QResizeEvent *e)
{
    QMainWindow::resizeEvent(e);

    ui->tabWidget->onRefresh();
}



void MainWindow::on_actionSave_triggered()
{
    ui->tabWidget->onSave();
}



void MainWindow::on_actionSave_As_triggered()
{
    ui->tabWidget->onSaveAs();
}



void MainWindow::on_actionOpen_triggered()
{
    ui->tabWidget->onOpen();
    ui->tabWidget->onRefresh();
}



void MainWindow::on_actionNew_triggered()
{
    ui->tabWidget->onNew();
    ui->tabWidget->onRefresh();
}



void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->onClose(index);
    ui->tabWidget->onRefresh();
}



void MainWindow::on_actionClose_Current_triggered()
{
    int index = ui->tabWidget->currentIndex();
    on_tabWidget_tabCloseRequested(index);
}



void MainWindow::on_actionQuit_triggered()
{
    int tabs = ui->tabWidget->count();
    for (int index = 0; index < tabs; index++)
    {
        on_tabWidget_tabCloseRequested(index);
    }

    close();
}



void MainWindow::on_actionRename_triggered()
{
    ui->tabWidget->onBeginRename(ui->tabWidget->currentIndex());
}



void MainWindow::on_actionRefresh_triggered()
{
    ui->tabWidget->onRefresh();
}




void MainWindow::on_actionDescription_toggled(bool )
{
}



void MainWindow::on_actionAdd_Group_triggered()
{
    ui->tabWidget->onAddGroup();
}



void MainWindow::on_actionAdd_Item_triggered()
{
    ui->tabWidget->onAddItem();
}



void MainWindow::on_actionAdd_Reference_triggered()
{
    ui->tabWidget->onAddReference();
    return;
}



void MainWindow::on_actionBegin_Edit_triggered()
{
    QTreeView* view = ui->tabWidget->tabView();

    if (!view) return;

    QModelIndex index = view->currentIndex();
    if (!index.isValid()) return;

    view->edit(index);
}


void MainWindow::on_actionShow_Details_triggered()
{
    QTreeView* view = ui->tabWidget->tabView();
    if (!view) return;

    QModelIndex index = view->currentIndex();
    AbstractItem* tmpItem = static_cast<AbstractItem*>(index.internalPointer());
    Item* item = dynamic_cast<Item*>(tmpItem);
    if (!item) return;

    ItemDetailsForm d(this);

    d.setItem(item, view->model());
    d.exec();
}
